<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reserve extends CI_Controller {

     public function __construct(){
        parent::__construct();
        $this->load->model('LoginModel');        

        if(!$this->LoginModel->checkLogin()){
            $this->json(['code'=>-1, 'data'=>'not login']);
        }
    }


    public function lists()
    {
        //$this->load->view('welcome_message');
        $type= $this->input->get('type');
        
        $this->load->model('ReserveModel');
        if($type =='reserve'){
            $list = $this->ReserveModel->getReserveList();
        }else{
            $list = $this->ReserveModel->getList();
        }

        $rs = true;
        if($rs){
            $this->json([
                'code' => 0,
                'msg' => "success", 
                'data' => $list, 
            ]);
        }else{
            $this->json([
                'code' => -1,
                'data' => "fail" 
            ]);
        }
    }


    public  function add(){
        $userId = $this->LoginModel->getUserWxId(); 
        $formId= $this->input->post('formId');  //微信提交表单ID，用于推送消息
        $shiftId = $this->input->post('shiftId');
        $operationTime = date("Y-m-d H:i:s", time());

        $this->load->model('ReserveModel');


        //check if reserved
        $reserved = $this->ReserveModel->checkReserved($userId, $shiftId);
        if($reserved){
            return $this->json(['code'=>-1, 'msg'=>'seat reserved']);
        }

        //check if seatId and shiftId exist
        $check = $this->ReserveModel->getSeat($userId, $shiftId);

        if($check['code'] == -1){
            return $this->json(['code'=>-1, 'msg'=>'seat not avail']);
        }
    

        $result = $this->ReserveModel->add($userId, $check['seatNum'], $shiftId, $operationTime, $formId);
        if(!$result){
            return $this->json(['code'=>-1, 'msg'=>'fail']);
        }else{
            return $this->json(['code'=>0, 'msg'=>'success', 'data'=>array('seatNum'=>$check['seatNum'])]);
        }
    }
    public function cancel(){

        $userId = $this->LoginModel->getUserWxId(); 
        $shiftId = $this->input->post('shiftId');

        $this->load->model('ReserveModel');

        $check = $this->ReserveModel->checkCancel($userId, $shiftId);
        if(!$check){
            return $this->json(['code'=>-1, 'msg'=>'seat could not cancel']);
        }
        
        $cancelTime = date("Y-m-d H:i:s", time());

        $result = $this->ReserveModel->cancel($userId, $shiftId, $cancelTime);
        if(!$result){
            return $this->json(['code'=>-1, 'msg'=>'fail']);
        }else{
            return $this->json(['code'=>0, 'msg'=>'success']);
        }
    }

    //司机点击发车
    public function start(){
        
        //提前发车时间
        $userId = $this->LoginModel->getUserWxId(); 
        $shiftId = $this->input->post('shiftId');

        $this->load->model('ShiftModel');
        $result = $this->ShiftModel->setStartTime($userId, $shiftId, time());

        if(!$result){
            return $this->json(['code'=>-1, 'msg'=>'fail']);
        }else{
            return $this->json(['code'=>0, 'msg'=>'success']);
        }
    } 

    //司机点击确认到达地点for driver to confirm the schedule
    public function arrive(){
        //提前发车时间
        $userId = $this->LoginModel->getUserWxId(); 
        $shiftId = $this->input->post('shiftId');

        $this->load->model('ShiftModel');
        $result = $this->ShiftModel->setArriveTime($userId, $shiftId, time());
        if(!$result){
            return $this->json(['code'=>-1, 'msg'=>'fail']);
        }else{
            return $this->json(['code'=>0, 'msg'=>'success']);
        }
    }
}
